Skip to main content

Python Lists

banner-python

๐Ÿ Let's Get Loopy with Python Lists ๐ŸŽ‰โ€‹

Welcome to your Python List Adventure โ€” where boring bullet points turn into belly laughs! Buckle up, we're about to get list-y!


๐Ÿค“ What Even Is a List?โ€‹

In Python, a list is like a magical backpack ๐Ÿงณ that:

  • โœ… Remembers the order you put stuff in (ordered)
  • ๐Ÿงฎ Keeps everything neat with numbers starting from 0 (indexed)
  • ๐Ÿ”„ Let's you swap things in and out like a mood ring (mutable)
  • ๐Ÿง™ Can hold a unicorn, a potato, and a disco ball all at once (heterogeneous)
  • ๐Ÿงฑ Is created using square brackets [] (because... geometry!)

1๏ธโƒฃ Creating a List โ€“ The Birth of Brillianceโ€‹

# Empty backpack
empty = []

# List of super fun subjects
listOfSubjects = ['physics', 'chemistry', "mathematics"]

# List of ID badges
listOfIds = [0, 1, 2, 3, 4]

# A random party of types
miscList = [0, 'one', 2, 'three']

# Nested mayhem: lists within lists
lists = [['A', 'B', 'C'], ['D', 'E', 'F']]

2๏ธโƒฃ Adding Stuff โ€“ Make Your List a VIP Club ๐Ÿฐโ€‹

charList = []

charList.append("a")
charList.append("b")

print(charList) # ['a', 'b']

# Insert 'c' at index 3... wait, list only has 2 items? Pythonโ€™s chill with that.
charList.insert(3, "c")

print(charList) # ['a', 'b', 'c']

# Try to insert at index 10. No explosions here.
charList.insert(10, "d")

print(charList) # ['a', 'b', 'c', 'd']

3๏ธโƒฃ Accessing Items โ€“ The Listโ€™s Peek-a-Boo Game ๐Ÿ‘€โ€‹

numList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(numList[0]) # 0
print(numList[1:5]) # [1, 2, 3, 4]
print(numList[:3]) # [0, 1, 2]
print(numList[7:]) # [7, 8, 9]
print(numList[-8:-5]) # [2, 3, 4] (Reverse magic!)

4๏ธโƒฃ Modify Like a List Surgeon ๐Ÿ‘จโ€โš•๏ธโ€‹

charList = ["a", "b", "c"]

charList[2] = "d"

print(charList) # ['a', 'b', 'd']

5๏ธโƒฃ Iterating โ€“ Say Hello to Every List Item ๐Ÿ‘‹โ€‹

charList = ["a", "b", "c"]

for x in charList:
print(x)
# Output:
# a
# b
# c

๐Ÿ“Œ P.S. Pythonโ€™s sensitive! Indent properly or face the wrath of indentation errors.


6๏ธโƒฃ Is it In the List or Not? The Suspense! ๐Ÿ˜ฑโ€‹

charList = ["a", "b", "c"]

if "a" in charList:
print("a is present")

if "d" in charList:
print("d is present")
else:
print("d is NOT present")

7๏ธโƒฃ Size Matters โ€“ How Long is My List? ๐Ÿ“โ€‹

charList = ["a", "b", "c"]
print(len(charList)) # 3

8๏ธโƒฃ Delete Like a Ninja โ€“ Bye Bye Items! ๐Ÿ‘‹โ€‹

๐Ÿงฝ 8.1 remove() โ€“ Yeet by Valueโ€‹

charList = ["a", "b", "c"]
charList.remove("c")
print(charList) # ['a', 'b']

๐Ÿฟ 8.2 pop() โ€“ Pull Out by Index or Lastโ€‹

charList = ["a", "b", "c", "d"]
charList.pop() # 'd' gone
print(charList) # ['a', 'b', 'c']
charList.pop(1) # 'b' gone
print(charList) # ['a', 'c']

๐Ÿ’จ 8.3 clear() โ€“ List Dusting 101โ€‹

charList = ["a", "b", "c", "d"]
charList.clear()
print(charList) # []

๐Ÿ’ฃ 8.4 del โ€“ The List Assassinโ€‹

charList = ["a", "b", "c", "d"]
del charList[0]
print(charList) # ['b', 'c', 'd']
del charList
# print(charList) # ๐Ÿ’ฅ Boom! NameError

9๏ธโƒฃ Merging Lists โ€“ When Two Become One โค๏ธโ€‹

charList = ["a", "b", "c"]
numList = [1, 2, 3]

joinedList = charList + numList
print(joinedList) # ['a', 'b', 'c', 1, 2, 3]

charList.extend(numList)
print(charList) # ['a', 'b', 'c', 1, 2, 3]

๐Ÿ”Ÿ List Methods โ€“ The Fancy Toolbox ๐Ÿ”งโ€‹

๐Ÿง append()โ€‹

charList = ["a", "b", "c"]
charList.append("d")
print(charList) # ['a', 'b', 'c', 'd']

๐Ÿงผ clear()โ€‹

charList = ["a", "b", "c"]
charList.clear()
print(charList) # []

๐Ÿชž copy()โ€‹

charList = ["a", "b", "c"]
newList = charList.copy()
print(newList) # ['a', 'b', 'c']

๐Ÿ”ข count()โ€‹

charList = ["a", "b", "c"]
print(charList.count('a')) # 1

๐Ÿงฉ extend()โ€‹

charList = ["a", "b", "c"]
numList = [1, 2, 3]
charList.extend(numList)
print(charList) # ['a', 'b', 'c', 1, 2, 3]

๐Ÿ” index()โ€‹

charList = ["a", "b", "c"]
print(charList.index('a')) # 0

๐Ÿง™ insert()โ€‹

charList = ["a", "b", "c"]
charList.insert(3, 'd')
print(charList) # ['a', 'b', 'c', 'd']

๐Ÿฟ pop()โ€‹

charList = ["a", "b", "c", "d"]
charList.pop()
print(charList) # ['a', 'b', 'c']
charList.pop(1)
print(charList) # ['a', 'c']

๐Ÿ—‘๏ธ remove()โ€‹

charList = ["a", "b", "c", "d"]
charList.remove('d')
print(charList) # ['a', 'b', 'c']

๐Ÿ”„ reverse()โ€‹

charList = ["a", "b", "c", "d"]
charList.reverse()
print(charList) # ['d', 'c', 'b', 'a']

๐Ÿง™โ€โ™‚๏ธ sort()โ€‹

charList = ["a", "c", "b", "d"]
charList.sort()
print(charList) # ['a', 'b', 'c', 'd']

๐ŸŽ“ Thatโ€™s All, Folks! ๐ŸŽฌโ€‹

Keep calm and append() on. Python lists are flexible, powerful, and fun โ€” kinda like duct tape, but for data!

Happy Learning & Happy List-ing! ๐Ÿ๐ŸŽ‰